package demo.demo;


import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/CalServlet")
public class Calculator extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Calculator() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String num1 = request.getParameter("num1");
        String num2 = request.getParameter("num2");

        String add = request.getParameter("add");
        String sub = request.getParameter("sub");
        String mul = request.getParameter("mul");
        String div = request.getParameter("div");

        int result = 0;
        if(add != null){
            result = Integer.parseInt(num1)+Integer.parseInt(num2);	//+
            System.out.println("加法运算");
        }

        if(sub != null){
            result = Integer.parseInt(num1)-Integer.parseInt(num2);	//+
            System.out.println("减法运算");
        }

        if(mul != null){
            result = Integer.parseInt(num1)*Integer.parseInt(num2);	//+
            System.out.println("乘法运算");
        }

        if(div != null){
            result = Integer.parseInt(num1)/Integer.parseInt(num2);	//+
            System.out.println("除法运算");
        }

        HttpSession session = request.getSession();
        session.setAttribute("result", result);

        RequestDispatcher dispatcher = request.getRequestDispatcher("Result.jsp");
        dispatcher.forward(request, response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}